package cfca.seal.web.tag;

import cfca.seal.bean.AdminUser;
import cfca.seal.bean.User;
import cfca.seal.dao.main.ISealDao;
import cfca.seal.dao.main.IUserDao;
import cfca.seal.web.system.SessionManager;
import cfca.seal.web.tag.base.TemplateDirectiveModelBase;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class UserBindTag extends TemplateDirectiveModelBase
{

  @Resource
  ISealDao sealDao;

  @Resource
  private IUserDao userDao;

  private Set<String> list2Set(List<String> list)
  {
    Set set = new LinkedHashSet();
    if (list != null) {
      for (int i = 0; i < list.size(); i++) {
        String str = (String)list.get(i);
        if (!set.contains(str)) {
          set.add(str);
        }
      }
    }
    return set.size() == 0 ? null : set;
  }

  private List<String> stringArrayToList(String[] strArray)
  {
    List list = new ArrayList();
    if (strArray != null) {
      for (int i = 0; i < strArray.length; i++) {
        list.add(strArray[i]);
      }
    }
    return list;
  }

  @Override
  public String process(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    Set userIdSet = list2Set(stringArrayToList(this.elementValue.split(",")));
    AdminUser manager = SessionManager.getManager(this.request);

    String organizationId = manager.getUserType() > 0 ? manager.getOrganizationId() : "";
    List userList = null;
    try {
      userList = this.userDao.getAll(organizationId);
    } catch (Exception e) {
      e.printStackTrace();
    }

    String tdPrefix = "<td style=\"padding-left:0px;background-color: white;border: 0 solid white;\">";
    String inputPrefix = "<input type=\"checkbox\"  style=\"vertical-align: -2px;\" name=\"" + this.elementName + "\" value=\"";

    StringBuffer sf = new StringBuffer();
    sf.append("<table cellspacing=\"0\" cellpadding=\"0\"  border=\"0\" id = \"").append(this.elementId).append("\" class = \"").append(this.className).append("\">");

    if (userList != null) {
      for (int i = 0; i < userList.size(); i++) {
        User user = (User)userList.get(i);
        sf.append("<tr>").append(tdPrefix).append(inputPrefix).append(user.getId()).append("\" ");

        if ((userIdSet != null) && (userIdSet.contains(user.getId()))) {
          sf.append("checked = \"checked\" ");
        }

        StringBuffer message = new StringBuffer();
        message.append("&nbsp;</td>").append(tdPrefix).append("用户名：" + user.getName()).append("&nbsp;</td>").append(tdPrefix).append("姓名：" + user.getRealName()).append("&nbsp;</td>").append(tdPrefix).append("证书SN：" + user.getCertSn()).append("&nbsp;</td>").append(tdPrefix).append("证书DN：" + user.getCertDn());

        sf.append(" />&nbsp;").append(message.toString()).append("</td></tr>");
      }
    }

    sf.append("</table>");
    return sf.toString();
  }
}